package cn.xopencode.recordservice.service.sf6machine;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.xopencode.common.framework.exception.util.ServiceExceptionUtil;
import cn.xopencode.common.framework.util.StringUtils;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.recordservice.convert.sf6machine.Sf6MachineRecordConvert;
import cn.xopencode.recordservice.convert.sf6machine.Sf6MachineRecordEmpRelConvert;
import cn.xopencode.recordservice.dal.mysql.dataobject.pdroom.PowerDistributionRoomDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordDetailDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordEmpRelDO;
import cn.xopencode.recordservice.dal.mysql.dataobject.sf6machine.Sf6MachineRecordOtherDO;
import cn.xopencode.recordservice.dal.mysql.mapper.pdroom.PowerDistributionRoomMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.sf6machine.Sf6MachineRecordDetailMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.sf6machine.Sf6MachineRecordEmpRelMapper;
import cn.xopencode.recordservice.dal.mysql.mapper.sf6machine.Sf6MachineRecordOtherMapper;
import cn.xopencode.recordservice.rpc.sf6machine.vo.Sf6MachineRecordNextVO;
import cn.xopencode.recordservice.rpc.sf6machine.vo.Sf6MachineRecordPageVO;
import cn.xopencode.recordservice.rpc.sf6machine.vo.Sf6MachineRecordVO;
import cn.xopencode.recordservice.service.sf6machine.bo.Sf6MachineRecordBO;
import cn.xopencode.recordservice.service.sf6machine.bo.Sf6MachineRecordNextCreateBO;
import cn.xopencode.recordservice.service.sf6machine.bo.Sf6MachineRecordOtherPageBO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.xopencode.recordservice.enums.RecordErrorCodeConstants.*;

/**
 * SF6气体密度继电器巡检记录 service
 */
@Service
@Validated
public class Sf6MachineRecordService {

    @Autowired
    private Sf6MachineRecordOtherMapper sf6MachineRecordOtherMapper;
    @Autowired
    private Sf6MachineRecordEmpRelMapper sf6MachineRecordEmpRelMapper;
    @Autowired
    private Sf6MachineRecordDetailMapper sf6MachineRecordDetailMapper;
    @Autowired
    private PowerDistributionRoomMapper powerDistributionRoomMapper;

    public PageResult<Sf6MachineRecordPageVO> pageSf6MachineRecord(Sf6MachineRecordOtherPageBO pageBO){
        IPage<Sf6MachineRecordOtherDO> otherDOIPage = sf6MachineRecordOtherMapper.selectPage(pageBO);
        PageResult<Sf6MachineRecordPageVO> pageVO = Sf6MachineRecordConvert.INSTANCE.convertPage(otherDOIPage);
        List<Sf6MachineRecordPageVO> list = pageVO.getList();
        if(!CollectionUtil.isEmpty(list)){
            //查询关联的巡检人记录
            for (Sf6MachineRecordPageVO vo : list){
                List<Sf6MachineRecordEmpRelDO> empRelDOList = sf6MachineRecordEmpRelMapper.selectListByRecordCode(vo.getRecordCode());
                vo.setEmpRelVOList(Sf6MachineRecordEmpRelConvert.INSTANCE.convert(empRelDOList));
            }
        }
        return pageVO;
    }

    public String createSf6MachineRecord(Sf6MachineRecordBO recordBO){
        Sf6MachineRecordOtherDO otherDO = Sf6MachineRecordConvert.INSTANCE.convert(recordBO);
        Sf6MachineRecordDetailDO detailDO = Sf6MachineRecordConvert.INSTANCE.convert2(recordBO);
        String recordCode = IdUtil.objectId();

        //保存 SF6气体密度继电器巡检记录 其余字段
        otherDO.setRecordCode(recordCode);
        otherDO.setId(IdUtil.objectId());
        sf6MachineRecordOtherMapper.insert(otherDO);

        //保存关联的巡检人和负责人信息
        Set<String> empIds = recordBO.getEmpIds();
        if (!CollectionUtil.isEmpty(empIds)) {
            List<Sf6MachineRecordEmpRelDO> empRels = empIds.stream()
                    .map(empId -> new Sf6MachineRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId)
                            .setRecordCode(recordCode).setManager(false)).collect(Collectors.toList());
            //负责人
            Sf6MachineRecordEmpRelDO empRel = new Sf6MachineRecordEmpRelDO();
            empRel.setId(IdUtil.objectId());
            empRel.setManager(true);
            empRel.setRecordCode(recordCode);
            empRel.setEmpId(recordBO.getLeaderEmp());
            empRels.add(empRel);
            sf6MachineRecordEmpRelMapper.insertList(empRels);
        }

        //保存 SF6气体密度继电器巡检记录明细
        detailDO.setRecordCode(recordCode);
        detailDO.setLeaderDeal(false);
        detailDO.setId(IdUtil.objectId());
        sf6MachineRecordDetailMapper.insert(detailDO);

        return recordCode;
    }

    public Integer createSf6MachineRecordNextLine(Sf6MachineRecordNextCreateBO nextCreateBO){
        //下一组保存时需要检查是否重复
        Sf6MachineRecordDetailDO detailDO = sf6MachineRecordDetailMapper.getSf6MachineRecordDetailByRecordCodeAndNum(nextCreateBO);
        if(detailDO != null && !StringUtils.isEmpty(detailDO.getId())){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_EXITS);
        }
        Sf6MachineRecordDetailDO detailDO1 = Sf6MachineRecordConvert.INSTANCE.convert(nextCreateBO);
        detailDO1.setId(IdUtil.objectId());
        return sf6MachineRecordDetailMapper.insert(detailDO1);
    }


    public Integer updateSf6MachineRecord(Sf6MachineRecordBO recordBO){
        Sf6MachineRecordOtherDO otherDO = Sf6MachineRecordConvert.INSTANCE.convert(recordBO);
        Sf6MachineRecordDetailDO detailDO = Sf6MachineRecordConvert.INSTANCE.convert2(recordBO);

        String recordCode = recordBO.getRecordCode();
        //保存 SF6气体密度继电器巡检记录 其余字段
        sf6MachineRecordOtherMapper.updateById(otherDO);

        //先删除所有关联的巡检人
        sf6MachineRecordEmpRelMapper.deleteByRecordCode(recordCode);

        //保存关联的巡检人和负责人信息
        Set<String> empIds = recordBO.getEmpIds();
        if (!CollectionUtil.isEmpty(empIds)) {
            List<Sf6MachineRecordEmpRelDO> empRels = empIds.stream()
                    .map(empId -> new Sf6MachineRecordEmpRelDO().setId(IdUtil.objectId()).setEmpId(empId)
                            .setRecordCode(recordCode).setManager(false)).collect(Collectors.toList());
            //负责人
            Sf6MachineRecordEmpRelDO empRel = new Sf6MachineRecordEmpRelDO();
            empRel.setId(IdUtil.objectId());
            empRel.setManager(true);
            empRel.setRecordCode(recordCode);
            empRel.setEmpId(recordBO.getLeaderEmp());
            empRels.add(empRel);
            sf6MachineRecordEmpRelMapper.insertList(empRels);
        }

        //保存 SF6气体密度继电器巡检记录明细
        detailDO.setId(recordBO.getRecordDetailId());
        return sf6MachineRecordDetailMapper.updateById(detailDO);
    }

    public Integer updateSf6MachineRecordNextLine(Sf6MachineRecordNextCreateBO nextCreateBO){
        //下一组保存时需要检查是否重复，校验重复不包含当条数据
        Sf6MachineRecordDetailDO detailDO = sf6MachineRecordDetailMapper.getSf6MachineRecordDetailByRecordCodeAndNum(nextCreateBO);
        if(detailDO != null && !detailDO.getId().equals(nextCreateBO.getId()) && !StringUtils.isEmpty(detailDO.getId())){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_EXITS);
        }Sf6MachineRecordDetailDO detailDO1 = Sf6MachineRecordConvert.INSTANCE.convert(nextCreateBO);
        return sf6MachineRecordDetailMapper.updateById(detailDO1);
    }

    public Sf6MachineRecordVO getSf6MachineRecordById(String recordId){
        Sf6MachineRecordOtherDO otherDO = sf6MachineRecordOtherMapper.selectById(recordId);
        if(otherDO == null){
            throw  ServiceExceptionUtil.exception(RECORD_SF6MACHINE_QUERY_ERROR);
        }
        Sf6MachineRecordVO recordVO = Sf6MachineRecordConvert.INSTANCE.convert(otherDO);

        Sf6MachineRecordDetailDO detailDO = sf6MachineRecordDetailMapper.getFirstSf6MachineRecordDetailByRecordCode(otherDO.getRecordCode(), 1);
        recordVO.setPdroomId(detailDO.getPdroomId());
        recordVO.setMpa(detailDO.getMpa());
        recordVO.setRemark(detailDO.getRemark());
        recordVO.setNoticeLeader(detailDO.getNoticeLeader());

        //获取序号和继电器位置（线路名称）
        PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(detailDO.getPdroomId());
        if(roomDO == null){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_PDROOM_NOT_EXITS2);
        }
        recordVO.setNum(roomDO.getNum());
        recordVO.setLineName(roomDO.getRoomName());

        //巡检位置
        PowerDistributionRoomDO roomDO2 = powerDistributionRoomMapper.selectById(otherDO.getRoomId());
        if(roomDO2 == null){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_PDROOM_NOT_EXITS);
        }
        recordVO.setPdroomName(roomDO2.getRoomName());

        //巡检人和负责人
        List<Sf6MachineRecordEmpRelDO> empRelDOList = sf6MachineRecordEmpRelMapper.selectListByRecordCode(otherDO.getRecordCode());

        return recordVO;
    }

    public Sf6MachineRecordNextVO getSf6MachineRecordNextById(String recordCode, Integer recordSeq){
        Sf6MachineRecordDetailDO detailDO = sf6MachineRecordDetailMapper.getFirstSf6MachineRecordDetailByRecordCode(recordCode, recordSeq);
        Sf6MachineRecordNextVO nextVO = Sf6MachineRecordConvert.INSTANCE.convert(detailDO);

        //获取序号和继电器位置（线路名称）
        PowerDistributionRoomDO roomDO = powerDistributionRoomMapper.selectById(detailDO.getPdroomId());
        nextVO.setNum(roomDO.getNum());
        nextVO.setLineName(roomDO.getRoomName());
        return nextVO;
    }

    public Integer dealSf6MachineRecordLineException(String recordDetailId){
        Sf6MachineRecordDetailDO detailDO = sf6MachineRecordDetailMapper.selectById(recordDetailId);
        if(detailDO == null){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_DETAIL_NOT_EXITS);
        }
        if(detailDO != null && detailDO.getNoticeLeader() != true){
            throw ServiceExceptionUtil.exception(RECORD_SF6MACHINE_DETAIL_NOT_EXCEPTION);
        }
        detailDO.setLeaderDeal(true);
       return sf6MachineRecordDetailMapper.updateById(detailDO);
    }
}
